#pragma once

#include "SDL/SDL.h"
#include "srect.h"
#include "templatearray.h"
#include "graphics.h"

class PixelFont
{
private:
	/** unmanaged surface */
	SDL_Surface * m_surface;
	/** list of where to find all printable letters in this font */
	TemplateArray<SRect> m_letters;
	/** first letter (inclusive) */
	int m_start;
	/** last letter (exclusive) */
	int m_end;
public:
	PixelFont():m_surface(0),m_start(0),m_end(0){}

	/** @return true if this font can handle the given letter */
	bool isPrintable(const int & a_letter){return a_letter >= m_start && a_letter < m_end;}

	SDL_Surface * getImageSource(){return m_surface;}
	/** where in the pixelfont's image to find the given letter */
	SRect getImageSourceOf(const char & a_letter)
	{
		return m_letters.get(a_letter-m_start);
	}
	/**
	 * @param a_surface source of the pixel font. Note: will not be released by this class
	 * @param a_tileSize size of each mono-space character, laid out in a grid
	 * @param a_startingLetter what is the first printable letter (0th letter in the source image)
	 * @param a_numLetters how many printable letters this pixel font has
	 */
	void setMonoSpaceAscii(SDL_Surface * a_surface, const SPoint & a_tileSize, 
		const int & a_startingLetter, const int & a_numLetters)
	{
		m_surface = a_surface;
		m_start = a_startingLetter;
		m_end = m_start + a_numLetters;
		m_letters.allocateToSize(a_numLetters);
		SPoint cursor;
		for(int i = 0; i < a_numLetters; ++i)
		{
			m_letters.get(i).set(cursor, cursor.sum(a_tileSize));
			cursor.addX(a_tileSize.getX());
			if(cursor.getX()+a_tileSize.getX() > m_surface->w)
			{
				cursor.setX(0);
				cursor.addY(a_tileSize.getY());
			}
		}
	}
	void setVariableWidthAscii(SDL_Surface * a_surface, 
		const int & a_startingLetter, const int & a_numLetters);

	void release()
	{
		m_letters.release();
		m_start = 0;
		m_end = 0;
		m_surface = 0;
	}
	~PixelFont()
	{
		release();
	}

	void drawGlyph(SDL_Surface * a_destination, 
		const int & tile, const SPoint & cursor);

	/** @return height of any letter */
	int getHeight()
	{
		return getImageSourceOf(' ').getHeight();
	}
};